package com.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.erp.entity.Admin;
import com.example.erp.mapper.AdminMapper;
import com.example.erp.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements IAdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Override
    public int doLogin(String name, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(Map.of("name", name, "password", password));
        Admin admin=adminMapper.selectOne(queryWrapper);
        if (admin!=null){
            return admin.getAdminId();
        }
        return 0;
    }

    @Override
    public boolean checkUsername(String name) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        Admin admin=adminMapper.selectOne(queryWrapper);
        if (admin!=null)return true;
        return false;
    }

    @Override
    public void register(String name, String password) {
        Admin admin=new Admin();
        admin.setName(name);
        admin.setPassword(password);
        adminMapper.insert(admin);
    }
}
